/**
 * @file src/DotBracket.cpp
 *
 * @date 14-may-2013
 *
 * @author Youri Hoogstrate
 * @author Lisa Yu
 *
 * @section LICENSE
 * yh-kt-fold can predict RNA 2D structures including K-turns.
 * Copyright (C) 2012-2013 Youri Hoogstrate
 *
 * This file is part of yh-kt-fold.
 *
 * yh-kt-fold is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * yh-kt-fold is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */



#include "DotBracket.h"
#include "Functions/library.h"



/**
 * @brief Constructs the class by setting all basepairs to UNBOUND.
 *
 * @date 14-may-2013
 */
DotBracket::DotBracket()
{
	this->initPairings();
	this->position = 0;
}



/**
 * @brief Initialize a new DotBracket object; set all basepairs to "UNBOUND"
 *
 * @date 14-may-2013
 */
void DotBracket::initPairings()
{
	for(int i = 0; i < MAXLEN; i++)
	{
		this->basepairs[i][0] = UNBOUND;
		this->basepairs[i][1] = UNBOUND;
	}
}



/**
 * @brief Searches for nucleotide pairing with i
 *
 * @param i position i in the RNA sequence
 *
 * @date 14-may-2013
 *
 * @return The position in the sequence pairing with the nucleotide at position i, "UNBOUND" otherwise
 *
 * @todo Make this a WHILE loop and avoid the early returns
*/
int DotBracket::find(int arg_i)
{
	int i;
	
	for(i = 0; i < min(this->position, MAXLEN); i++)
	{
		if(this->basepairs[i][0] == arg_i)
		{
			return this->basepairs[i][1];
		}
		else if (this->basepairs[i][1] == arg_i)
		{
			return this->basepairs[i][0];
		}
	}
	
	return UNBOUND;
}



/**
 * @brief Stores pairing (i,j)
 *
 * @param arg_i Nucleotide position in the RNA sequence pairing with (,j)
 * @param arg_j Nucleotide position in the RNA sequence pairing with (i,)
 *
 * @date 14-may-2013
*/
void DotBracket::store(int arg_i, int arg_j)
{
	if(this->position >= MAXLEN)
	{
		fprintf(stderr, "Reached maximum number of storable pairings in DotBracket object\n");
		exit(1);
	}
	else
	{
		this->basepairs[this->position][0] = arg_i;
		this->basepairs[this->position][1] = arg_j;
		
		this->position++;
	}
}



/**
 * @brief Formats and prints the 2D structure in DotBracket format
 *
 * @param rnaSequenceLength The length of the sequence of which the 2D strucutre is calculated
 *
 * @date 22-may-2013
 */
void DotBracket::format(int arg_rnaSequenceLength)
{
	int i, j;
	
	for(i = 0; i < arg_rnaSequenceLength; i++)
	{
		j = this->find(i);
		
		if(j == UNBOUND)
		{
			printf(DOTBRACKET__NO_PAIRING);								// "."
		}
		else if(j > i)
		{
			printf(DOTBRACKET__PAIRING_LEFT);							// "("
		}
		else if(j < i)
		{
			printf(DOTBRACKET__PAIRING_RIGHT);							// ")"
		}
	}
}
